<?php
/**
 * 
 *
 * @copyright	Copyright 2009-2013   
 * @license	LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author	XinHui Wang <84523559@qq.com>
 * @package	autoweb
 * @version	version
 * @link	http://hi.baidu.com/wangxinhui419/home
 */
class control extends STpl {

    public $params = array();
    
    protected $config=array();

    public function __construct()
    {
    	$this->config=config::readcof();
        $this->params['_time'] = time();
        $this->params['config']=$this->config;
    }

    /**
     * 构建完整url
     */
    public function createUrl($route, $params = array())
    {

        $root_dir = isset($this->config['website']['ROOT_DIR']) && !empty($this->config['website']['ROOT_DIR']) ? '/' . trim($this->config['website']['ROOT_DIR'], '/') : '';
        if(!$this->config['website']['URL_REWRITE'])
        {
        	$root_dir.='/index.php';
        }
        if(isset($this->config['website']['DEFAULT_CONTROL_URL']) && !empty($this->config['website']['DEFAULT_CONTROL_URL']))
        {
        	$root_dir.=$this->config['website']['DEFAULT_SPLITFLAG'].$this->config['website']['DEFAULT_CONTROL_URL'];
        }
        $uf = $this->config['website']['DEFAULT_SPLITFLAG'];
        $url = str_replace('/',$this->config['website']['DEFAULT_SPLITFLAG'],$route);
        $sux = $this->config['website']['URL_REWRITE'] && !empty($this->config['website']['URL_SUFFIX'])?'.' . $this->config['website']['URL_SUFFIX']:'';
        $tmp='';
        if (!empty($params)) {
        	$url=rtrim($url,'/');
            foreach ($params as $key => $value) {
                if (trim($value) != '') {
                    $tmp .= $key . $uf . $value . $uf;
                }
            }
            $tmp = rtrim($tmp, $uf);
            $url = rtrim($url . $this->config['website']['DEFAULT_SPLITFLAG'] . $tmp, $this->config['website']['DEFAULT_SPLITFLAG']);
        }
        if (substr($url, -1) != '/') { //以'/'结束的url不加$sux;
            $url = $route === '' ? $url : $url . $sux;
        }
        
        if (!$this->config['website']['URL_REWRITE']) {return   $root_dir . $url;}
        return $root_dir . $url;
    }
    
    
    public function render($tpl,$params=array())
    {
    	parent::$left_delimiter=$this->config['view']['left_delimiter'];
    	parent::$right_delimiter=$this->config['view']['right_delimiter'];
    	$params=array_merge($this->params,$params);
    	return parent::render($tpl,$params);
    }    

    /**
     * 系统提示
     */
    public function ShowMsg($msg = 'message', $backurl = '', $second = 3, $state = 0)
    {
        $params['msg'] = $msg;
        $params['url'] = $backurl;
        $params['second'] = $second;
        $params['state'] = $state;
        $this->render('common/showmsg.tpl', $params);
        exit();
    }
    

}
?>
